package com.example.csmall.product.controller;

import com.example.csmall.product.pojo.dto.AttributeAddNewDTO;
import com.example.csmall.product.pojo.dto.AttributeUpdateDTO;
import com.example.csmall.product.pojo.vo.AttributeListByTemplateIdVO;
import com.example.csmall.product.pojo.vo.AttributeListItemVO;
import com.example.csmall.product.pojo.vo.AttributeStandardVO;
import com.example.csmall.product.service.AttributeService;
import com.example.csmall.product.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "03 属性")
@Validated
@RequestMapping("/attributes")
public class AttributeController {

    @Autowired
    AttributeService attributeService;

    @PostMapping("/add-new")
    @ApiOperation("添加属性")
    public JsonResult<Void> addNew(@Valid AttributeAddNewDTO attributeAddNewDTO){
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改属性")
    public JsonResult<Void> updateById(@PathVariable Long id, AttributeUpdateDTO attributeUpdateDTO){
        attributeService.updateInfoById(id,attributeUpdateDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除属性")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        attributeService.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询数据")
    public JsonResult<AttributeStandardVO> getStandardById(@PathVariable Long id){
        AttributeStandardVO standardById = attributeService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("")
    @ApiOperation("数据列表")
    public JsonResult<List<AttributeListItemVO>> list(){
        List<AttributeListItemVO> list = attributeService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/{templateId:[0-9]+}/list-by-templateId")
    @ApiOperation("查询某个属性模板下的属性")
    public JsonResult<List<AttributeListByTemplateIdVO>> listByTemplateId(@PathVariable Long templateId){
        List<AttributeListByTemplateIdVO> list = attributeService.listByTemplateId(templateId);
        return JsonResult.ok(list);
    }
}
